package com.xiang.utils;

import com.xiang.config.EsConfig;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * 删除,查询
 *
 * @author xiangtianlei@gogpay.cn
 * @date 2023-01-30
 */
public class DeleteByQuery {
    RestHighLevelClient client = EsConfig.getClient();
    Logger logger = LoggerFactory.getLogger(RangeQuery.class);

    /**
     * 删除,查询
     */
    @Test
    public void deleteByQuery() throws IOException {
        // 创建deleteQuery对象
        DeleteByQueryRequest request = new DeleteByQueryRequest(EsConfig.INDEX_NAME);
        // 指定检索的条件
        request.setQuery(QueryBuilders.rangeQuery("age").lte(24));
        // 执行删除
        BulkByScrollResponse res = client.deleteByQuery(request, RequestOptions.DEFAULT);
        // 结果
        logger.info("结果：{}", res.toString());
    }
}
